﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 全选命令。
    /// ★①，修改类名。
    /// </summary>
    public static class SetTitleIconXamlTextCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static SetTitleIconXamlTextCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "SetTitleIconXamlTextCommand",
                "SetTitleIconXamlTextCommand",
                typeof(SetTitleIconXamlTextCommand),//创建RoutedUICommand对象
                null);

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。
            PasteXamlTextToTitle();
        }

        /// <summary>
        /// 此方法是调用DeleteCommand的Delete来实现。
        /// </summary>
        internal static string PasteXamlTextToTitle(string newXamlText = null)
        {
            if (Globals.MainWindow.SelectedTitlesList == null ||
                Globals.MainWindow.SelectedTitlesList.Count <= 0)
                return "未选定标题。";

            ModifingItem mi = new ModifingItem()
            {
                IsNeedRefreshTitleAndLinesLocation = true,
                ModifingName = "设置标题自定元素Xaml文本"
            };

            newXamlText = string.IsNullOrWhiteSpace(newXamlText) ? Clipboard.GetText(TextDataFormat.UnicodeText) : newXamlText;

            string tmptxt = newXamlText.Trim(new char[] { '\r', '\n', '\t', ' ', });

            if (tmptxt.StartsWith("<LunarMind.Xaml>") && tmptxt.EndsWith("</LunarMind.Xaml>"))
            {
                newXamlText = newXamlText.Replace("<LunarMind.Xaml>", "").Replace("</LunarMind.Xaml>", "");
            }
            else if (tmptxt.StartsWith("<Viewbox ") && tmptxt.EndsWith("</Viewbox>"))
            {
                newXamlText = tmptxt.Replace("<", "&lt;").Replace(">", "&gt;");
            }
            else if (tmptxt.StartsWith("<LunarConcept><Page") == false || tmptxt.EndsWith("</Page></LunarConcept>") == false)
            {
                return string.Empty;
            }

            try
            {
                foreach (Title t in Globals.MainWindow.SelectedTitlesList)
                {
                    Action actIndent = new Action(t.ID, -1, t.GetType().Name, "IconXamlText", t.IconXamlText,
                        newXamlText);

                    t.IconXamlText = newXamlText;

                    mi.Add(actIndent);
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            if (mi.ModifiedPropertyAndValues.Count <= 0) return "撤销列表出错。";

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
            Globals.MainWindow.RefreshTitleAndLinesLocation();
            return string.Empty;
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //if (Globals.MainWindow.SelectedTitlesList == null ||
            //    Globals.MainWindow.SelectedTitlesList.Count >= 0)
            //{
            //    e.CanExecute = false; return;
            //}
            //else
            //{
            //    e.CanExecute = true; return;
            //}

            e.CanExecute = true;
        }

        internal static void PasteXamlTextToTitle()
        {
            var text = Clipboard.GetText();
            if (text == null || text.Length <= 0)
            {
                var result = MessageBox.Show("剪贴板中无合适的Xaml文本！要粘贴一个示例吗？",
                     Globals.AppName, MessageBoxButton.YesNo, MessageBoxImage.Warning);

                if (result == MessageBoxResult.Yes)
                {
                    PasteXamlTextToTitle(Properties.Resources.ExampleXamlGrid);
                }
                return;
            }
            else
            {
                var returnText = PasteXamlTextToTitle(text);
                if (returnText != string.Empty)
                {
                    var result = MessageBox.Show("剪贴板中无合适的Xaml文本！要粘贴一个示例吗？",
                        Globals.AppName, MessageBoxButton.YesNo, MessageBoxImage.Warning);
                    if (result == MessageBoxResult.Yes)
                    {
                        PasteXamlTextToTitle(Properties.Resources.ExampleXamlGrid);
                    }
                }
                return;
            }
        }

        internal static string ClearXamlTextOfTitle()
        {
            if (Globals.MainWindow.SelectedTitlesList == null ||
                Globals.MainWindow.SelectedTitlesList.Count <= 0)
                return "未选定标题。";

            ModifingItem mi = new ModifingItem()
            {
                IsNeedRefreshTitleAndLinesLocation = true,
                ModifingName = "清除标题自定元素Xaml文本"
            };

            try
            {
                foreach (Title t in Globals.MainWindow.SelectedTitlesList)
                {
                    if (t.IconXamlText == "") continue;

                    Action actClearXaml = new Action(t.ID, -1, t.GetType().Name, "IconXamlText", t.IconXamlText, "");

                    t.IconXamlText = "";

                    mi.Add(actClearXaml);
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            if (mi.ModifiedPropertyAndValues.Count <= 0) return "撤销列表出错。";

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
            Globals.MainWindow.RefreshTitleAndLinesLocation();
            return string.Empty;
        }
    }
}
